package com.fbee.smartcommunities.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fbee.smartcommunities.entity.Permission;
import com.fbee.smartcommunities.service.PermissionService;
import com.fbee.smartcommunities.utils.CustomizeException;
import com.fbee.smartcommunities.vo.ResultVO;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 权限菜单表 前端控制器
 * </p>
 *
 * @author 汪佳泉
 * @since 2024-08-16
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    @GetMapping("/list")
    public ResultVO<List<Permission>> list(){
        return ResultVO.success(permissionService.getlist());
    }

    @GetMapping("/getById")
    public ResultVO<Permission> getById(Long id){
        return ResultVO.success(permissionService.getById(id));
    }

    @PostMapping("/del")
    @CacheEvict(value = "user", key = "'permission'")
    public ResultVO<Boolean> delById(Long id) throws CustomizeException {
        return ResultVO.success(permissionService.delete(id),"删除成功");
    }

    @PostMapping("/add")
    @CacheEvict(value = "user", key = "'permission'")
    public ResultVO<Boolean> add(String name, String description, Long parentId, String frontUrl, String backUrl, Integer level, Integer type){
        return ResultVO.success(permissionService.save(new Permission(name,description,parentId,frontUrl,backUrl,level,type)),"添加成功");
    }

    @PostMapping("/update")
    @CacheEvict(value = "user", key = "'permission'")
    public ResultVO<Boolean> update(Long id, String name, String frontUrl, String backUrl, String description, Integer type){
        return ResultVO.success(permissionService.update(id,name,frontUrl,backUrl,description,type),"修改成功");
    }

    @GetMapping("/check")
    public ResultVO<Boolean> check(String filedName, String value, Long id){
        return ResultVO.success(permissionService.check(filedName, value, id));
    }

    @GetMapping("/child")
    public ResultVO<Boolean> hasChild(Long id){
        int size = permissionService.list(new QueryWrapper<Permission>().eq("parent_id", id)).size();
        return ResultVO.success(size > 0 ? true : false);
    }


}
